#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_m_spbOriginalWeight_valueChanged(double arg1)
{
    m_oriData.weight = arg1;
    calculate();
    updateOriUI();
}


void MainWindow::on_m_spbOriginalFatRate_valueChanged(double arg1)
{
    m_oriData.fatRate = arg1;
    calculate();
    updateOriUI();
}


void MainWindow::on_m_spbOriginalMuscleRate_valueChanged(double arg1)
{
    m_oriData.muscleRate = arg1;
    calculate();
    updateOriUI();
}


void MainWindow::on_m_spbNowWeight_valueChanged(double arg1)
{
    m_nowData.weight = arg1;
    calculate();
    updateNowUI();
}


void MainWindow::on_m_spbNowFatRate_valueChanged(double arg1)
{
    m_nowData.fatRate = arg1;
    calculate();
    updateNowUI();
}


void MainWindow::on_m_spbNowMuscleRate_valueChanged(double arg1)
{
    m_nowData.muscleRate = arg1;
    calculate();
    updateNowUI();
}


void MainWindow::on_m_btnCalculate_clicked()
{
    calculate();
    updateUI();
}

void MainWindow::calculate()
{
    m_oriData.fatWeight = m_oriData.weight * m_oriData.fatRate / 100.0;
    m_oriData.muscleWeight = m_oriData.weight * m_oriData.muscleRate / 100.0;

    m_nowData.fatWeight = m_nowData.weight * m_nowData.fatRate / 100.0;
    m_nowData.muscleWeight = m_nowData.weight * m_nowData.muscleRate / 100.0;

    m_calculateData.subWeight = m_oriData.weight - m_nowData.weight;
    m_calculateData.subFatWeight = m_oriData.fatWeight - m_nowData.fatWeight;
    m_calculateData.subMuscleWeight = m_oriData.muscleWeight - m_nowData.muscleWeight;
    m_calculateData.subWaterWeight = m_calculateData.subWeight - m_calculateData.subFatWeight - m_calculateData.subMuscleWeight;

    m_calculateData.fatMuscleRate = m_calculateData.subMuscleWeight / m_calculateData.subFatWeight;
    m_calculateData.fatMuscleWaterRate = m_calculateData.subWaterWeight / m_calculateData.subFatWeight;
    m_calculateData.OneFatSub = 1 + m_calculateData.fatMuscleRate + m_calculateData.fatMuscleWaterRate;
}

void MainWindow::updateOriUI()
{
    ui->m_labOriginalFatWeight->setText(QString::number(m_oriData.fatWeight));
    ui->m_labOriginalMuscleWeight->setText(QString::number(m_oriData.muscleWeight));
}

void MainWindow::updateNowUI()
{
    ui->m_labNowFatWeight->setText(QString::number(m_nowData.fatWeight));
    ui->m_labNowMuscleWeight->setText(QString::number(m_nowData.muscleWeight));
}

void MainWindow::updateUI()
{
    ui->m_labSubWeight->setText(QString::number(m_calculateData.subWeight));
    ui->m_labSubFatWeight->setText(QString::number(m_calculateData.subFatWeight));
    ui->m_labMuscleWeight->setText(QString::number(m_calculateData.subMuscleWeight));
    ui->m_labSubWaterWeight->setText(QString::number(m_calculateData.subWaterWeight));

    ui->m_labFatMuscle->setText(QString("1 : %1").arg(m_calculateData.fatMuscleRate));
    ui->m_labFatMuscleWater->setText(QString("1 : %1 : %2").arg(m_calculateData.fatMuscleRate).arg(m_calculateData.fatMuscleWaterRate));
    ui->m_labOneFat->setText(QString::number(m_calculateData.OneFatSub));
}

